﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skylabs.Breeze
{
    public static class NetworkingStuff
    {
        public delegate void ListNetworkComputersComplete(List<DirectoryEntry> comps);
        public static void ListNetworkComputers(ListNetworkComputersComplete onComplete)
        {
            Task t = new Task(new Action(()=>
                {
                    List<DirectoryEntry> _ComputerNames = new List<DirectoryEntry>();
                    String _ComputerSchema = "Computer";
                    System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
                    foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children)
                    {
                        foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children)
                        {
                            if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower()))
                            {
                                _ComputerNames.Add(_PCNameEntry);
                            }
                        }
                    }
                    onComplete.Invoke(_ComputerNames);                                                 
                }));
            t.Start();
        }
    }
}
